package com.cloudsigns.factorymanager2.project.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IUploadPhotoView;
import com.cloudsigns.factorymanager2.engineering.bean.AccountInfoBean;
import com.cloudsigns.factorymanager2.engineering.bean.DialogItemBean;
import com.cloudsigns.factorymanager2.engineering.customview.BottomListDialog;
import com.cloudsigns.factorymanager2.engineering.presenter.UploadPhotoPresenter;
import com.cloudsigns.factorymanager2.engineering.utils.PhotoUtil;
import com.cloudsigns.factorymanager2.produce.iview.ICreateAccountView;
import com.cloudsigns.factorymanager2.produce.presenter.CreateAccountPresenter;
import com.cloudsigns.factorymanager2.project.customview.GeneralPurposePoupWindow;
import com.cloudsigns.factorymanager2.project.customview.GlideCircleTransform;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AddPersonnelSettingActivity1 extends BaseActivity implements ItemClickListener, BottomListDialog.OnItemDialogClick, IUploadPhotoView, ICreateAccountView {
    private String TAG = AddPersonnelSettingActivity1.class.getSimpleName();
    private ImageView img_back;
    private ImageView img_head;
    private TextView tv_title;
    private Button btn_save;
    private EditText edt_role_category, edt_user_name, edt_login_account;
    private ImageView img_maintenance_types_select;

    private List<String> picUrl;
    private String serverUrl;
    private String filePath;
    private UploadPhotoPresenter uploadPhotoPresenter;
    private CreateAccountPresenter createAccountPresenter;

    private GeneralPurposePoupWindow generalPurposePoupWindow;
    private String project_datas[] = {"项目经理", "项目助理", "项目执行"};

    private String engineer_datas[] = {"工程管理", "工程班组"};

    private String produce_datas[] = {"生产助理", "生产班组"};

    AccountInfoBean accountInfoBean = MyApplicaton.getApplication().getAccountInfoBean();

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_add_personnel_setting1);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        img_back = findViewById(R.id.img_back);
        img_head = findViewById(R.id.img_head);
        tv_title = findViewById(R.id.tv_title);
        btn_save = findViewById(R.id.btn_save);
        edt_role_category = findViewById(R.id.edt_role_category);
        edt_user_name = findViewById(R.id.edt_user_name);
        edt_login_account = findViewById(R.id.edt_login_account);
        img_maintenance_types_select = findViewById(R.id.img_maintenance_types_select);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.create_account));

        uploadPhotoPresenter = new UploadPhotoPresenter(this);
        createAccountPresenter = new CreateAccountPresenter(this);
    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(this);
        img_head.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        img_maintenance_types_select.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            // 返回
            case R.id.img_back:
                finish();
                break;
            // 头像
            case R.id.img_head:
                showListDialog();
                break;
            // 职位
            case R.id.img_maintenance_types_select:
                showMaintanceTypesWindow(edt_role_category);
                break;
            // 保存
            case R.id.btn_save:
                if (TextUtils.isEmpty(serverUrl)) {
                    showToast(getResources().getString(R.string.upload_head));
                    return;

                } else if (TextUtils.isEmpty(edt_role_category.getText().toString())) {
                    showToast(getResources().getString(R.string.choose_role_category));
                    return;

                } else if (TextUtils.isEmpty(edt_user_name.getText().toString())) {
                    showToast(getResources().getString(R.string.input_account_name));
                    return;

                } else if (TextUtils.isEmpty(edt_login_account.getText().toString())) {
                    showToast(getResources().getString(R.string.input_login_account));
                    return;

                } else {
                    createAccountPresenter.createAccount();
                }
                break;
        }
    }


    // 职位
    private void showMaintanceTypesWindow(View anchor) {
        AccountInfoBean accountInfoBean = MyApplicaton.getApplication().getAccountInfoBean();
        // 项目
        if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.SUPER) {
            generalPurposePoupWindow = new GeneralPurposePoupWindow(this, project_datas);
            generalPurposePoupWindow.setItemClick(this);
            generalPurposePoupWindow.showPopupWindow(anchor);
        }
        // 工程
        else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.MANAGER) {
            generalPurposePoupWindow = new GeneralPurposePoupWindow(this, engineer_datas);
            generalPurposePoupWindow.setItemClick(this);
            generalPurposePoupWindow.showPopupWindow(anchor);

            // 生产
//            generalPurposePoupWindow = new GeneralPurposePoupWindow(this, produce_datas);
//            generalPurposePoupWindow.setItemClick(this);
//            generalPurposePoupWindow.showPopupWindow(anchor);
        }
        // 班组
        else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.TEAM) {

        }
    }

    // 头像
    private void showListDialog() {
        DialogItemBean[] items = new DialogItemBean[]{
                new DialogItemBean(R.string.camera,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.gallary,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.cancel,
                        R.layout.dialog_list_item_normal)};
        BottomListDialog dialog = new BottomListDialog(this, Arrays.asList(items));
        dialog.setItemClick(this);
        dialog.show();
    }


    @Override
    public void itemClick(View view, int position) {
        AccountInfoBean accountInfoBean = MyApplicaton.getApplication().getAccountInfoBean();
        // 项目
        if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.SUPER) {
            edt_role_category.setText(project_datas[position]);
        }
        // 工程
        else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.MANAGER) {
            edt_role_category.setText(engineer_datas[position]);

            // 生产
//            edt_role_category.setText(produce_datas[position]);
        }
        // 班组
        else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.TEAM) {

        }
    }

    @Override
    public void onItemDialogClick(DialogItemBean item) {
        switch (item.getmTextId()) {
            case R.string.camera: {
                PhotoUtil.startCamear(AddPersonnelSettingActivity1.this, null);
                break;
            }
            case R.string.gallary: {
                PhotoUtil.startGallery(AddPersonnelSettingActivity1.this, null);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

            case PhotoUtil.CAMERA:
                if (resultCode == RESULT_OK) {
                    File picture = new File(PhotoUtil.filePath);
                    if (!picture.exists()) {
                        picture = new File(PhotoUtil.filePath);
                    }
                    filePath = PhotoUtil.CAMERA_SEVER + picture.getName();
                    filePath = PhotoUtil.imageCompressionAndSave(picture.getAbsolutePath(), filePath);

                    Log.e(TAG, "----filePath=" + filePath);

                    picUrl = new ArrayList<>();
                    picUrl.add(filePath);

                    Glide.with(this).load(new File(filePath)).bitmapTransform(new GlideCircleTransform(this)).crossFade(1000).error(R.drawable.icon_head_oval).into(img_head);
                    uploadPhotoPresenter.uploadPhtoto();
                }

                break;
            case PhotoUtil.GALLERY:
                if (resultCode == RESULT_OK) {
                    String path = FileUtil.getRealPathFromURI(this, Uri.parse(data.getData() + ""));
                    Log.e(TAG, "----path=" + path);

                    picUrl = new ArrayList<>();
                    picUrl.add(path);

                    Glide.with(this).load(new File(path)).bitmapTransform(new GlideCircleTransform(this)).crossFade(1000).error(R.drawable.icon_head_oval).into(img_head);

                    File f = new File(path);
                    filePath = PhotoUtil.CAMERA_SEVER + f.getName();
                    filePath = PhotoUtil.imageCompressionAndSave(path, filePath);
                    uploadPhotoPresenter.uploadPhtoto();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public String getFilePath() {
        return filePath;
    }

    @Override
    public void upLoadPhotoSuccessed(String uri) {
        serverUrl = uri;
    }

    @Override
    public void upLoadPhotoFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public CommonEnum.ROLECATEGORY getRoleCategory() {
        return accountInfoBean.getRoleCategory();
    }

    @Override
    public String getAvatar() {
        return serverUrl;
    }

    @Override
    public String getAccountId() {
        return edt_login_account.getText().toString();
    }

    @Override
    public String getAccountName() {
        return edt_user_name.getText().toString();
    }

    @Override
    public void getCreateAccountSuccessed() {
        Intent mIntent = new Intent();
        setResult(ConstantUtil.ADD_PERSONNEL_SETTING, mIntent);
        showToast(getResources().getString(R.string.create_account_success));
        finish();
    }

    @Override
    public void getCreateAccountFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
